package cn.sylinx.common.ext.web;

import java.io.Serializable;

import com.alibaba.fastjson.JSON;

/**
 * 系统返回页面结果
 * 
 * @author hanqz
 *
 */
@SuppressWarnings("serial")
public class Result implements Serializable {

	private String code;

	private Object data;

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public Object getData() {
		return data;
	}

	public void setData(Object data) {
		this.data = data;
	}

	public Result(String code, Object data) {
		this.code = code;
		this.data = data;
	}

	public static Result error500() {

		return new Result("500", "系统错误，请稍后重试！");
	}

	public static Result ok200() {

		return new Result("200", "已成功执行");
	}

	public static Result error404() {

		return new Result("404", "资源未找到");
	}

	public static Result apply(String code, Object data) {
		return new Result(code, data);
	}

	public static Result ok200(Object data) {
		return new Result("200", data);
	}

	public static Result error500(Object data) {
		return new Result("500", data);
	}

	public static Result error404(Object data) {

		return new Result("404", data);
	}

	public static String toJson(Result r) {
		return JSON.toJSONString(r);
	}

	public static String ok200ForJson() {

		return toJson(ok200());
	}

	public static String error404ForJson() {

		return toJson(error404());
	}

	public static String ok200ForJson(Object data) {
		return toJson(ok200(data));
	}

	public static String error500ForJson(Object data) {
		return toJson(error500(data));
	}

	public static String error404ForJson(Object data) {
		return toJson(error404(data));
	}

	public static Result needLogin() {
		return new Result("401", "未登录或者会话已超时");
	}

	public static String needLoginForJson() {
		return toJson(needLogin());
	}
}
